package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.commons.security.LoginPrincipal;
import cn.tedu.csmall.commons.web.JsonResult;
import cn.tedu.csmall.product.pojo.param.SpuAddNewParam;
import cn.tedu.csmall.product.pojo.param.SpuUpdateInfoParam;
import cn.tedu.csmall.product.pojo.vo.*;
import cn.tedu.csmall.product.service.ISpuService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.Ignore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Slf4j
@RestController
@Validated
@Api(tags = "07. SPU管理模块")
@RequestMapping("/spu")
public class SpuController {

    @Value("${csmall.crud.default-query-page-size}")
    private Integer defaultQuerySize;
    @Autowired
    private ISpuService spuService;

    // http://localhost:9180/spu/add-new
    @PostMapping("/add-new")
    @ApiOperation("新增SPU")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid SpuAddNewParam spuAddNewParam) {
        log.debug("开始处理【新增SPU】的请求，参数：{}", spuAddNewParam);
        spuService.addNew(spuAddNewParam);
        return JsonResult.ok();
    }

    // http://localhost:9180/spu/{id}/delete
    @PostMapping("/{id:^\\d+$}/delete")
    @ApiOperation("根据id删除SPU")
    @ApiOperationSupport(order = 200)
    public JsonResult delete(@Range(min = 1, message = "请输入合法的ID值！")
                             @PathVariable Long id) {
        log.debug("开始处理【根据id删除SPU】的请求，参数ID：{}", id);
        spuService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9180/spu/{id}/setIsNewArrival
    @PostMapping("/{id:^\\d+$}/setIsNewArrival")
    @ApiOperation("根据id设置SPU为新品")
    @ApiOperationSupport(order = 310)
    public JsonResult setIsNewArrival(@Range(min = 1, message = "请输入合法的ID值！")
                                      @PathVariable Long id) {
        log.debug("开始处理【根据id设置是否为新品】的请求，参数ID：{}", id);
        spuService.updateIsNewArrival(id);
        return JsonResult.ok();
    }

    // http://localhost:9180/spu/{id}/setNoNewArrival
    @PostMapping("/{id:^\\d+$}/setNoNewArrival")
    @ApiOperation("根据id设置SPU为非新品")
    @ApiOperationSupport(order = 320)
    public JsonResult setNoNewArrival(@Range(min = 1, message = "请输入合法的ID值！")
                                      @PathVariable Long id) {
        log.debug("开始处理【根据id设置SPU为非新品】的请求，参数ID：{}", id);
        spuService.updateNoNewArrival(id);
        return JsonResult.ok();
    }

    // http://localhost:9180/spu/{id}/setIsRecommend
    @PostMapping("/{id:^\\d+$}/setIsRecommend")
    @ApiOperation("根据id设置SPU为推荐")
    @ApiOperationSupport(order = 330)
    public JsonResult setIsRecommend(@Range(min = 1, message = "请输入合法的ID值！")
                                     @PathVariable Long id) {
        log.debug("开始处理【setIsRecommend】的请求，参数ID：{}", id);
        spuService.updateIsRecommend(id);
        return JsonResult.ok();
    }

    // http://localhost:9180/spu/{id}/setNoRecommend
    @PostMapping("/{id:^\\d+$}/setNoRecommend")
    @ApiOperation("根据id设置SPU为不推荐")
    @ApiOperationSupport(order = 340)
    public JsonResult setNoRecommend(@Range(min = 1, message = "请输入合法的ID值！")
                                     @PathVariable Long id) {
        log.debug("开始处理【根据id设置SPU为不推荐】的请求，参数ID：{}", id);
        spuService.updateNoRecommend(id);
        return JsonResult.ok();
    }

    // http://localhost:9180/spu/{id}/update
    @PostMapping("/{id:^\\d+$}/update")
    @ApiOperation("根据id修改SPU详情")
    @ApiOperationSupport(order = 350)
    public JsonResult update(@Range(min = 1, message = "请输入合法的ID值！")
                             @PathVariable Long id, SpuUpdateInfoParam spuUpdateInfoParam) {
        log.debug("开始处理【修改SPU详情】的请求，参数ID：{},更新的数据：{}", id, spuUpdateInfoParam);
        spuService.update(id, spuUpdateInfoParam);
        return JsonResult.ok();
    }

    // http://localhost:9180/spu/{id}
    @GetMapping("/{id:^\\d+$}")
    @ApiOperation("根据id获取SPU详情")
    @ApiOperationSupport(order = 400)
    public JsonResult getStandard(@Range(min = 1, message = "请输入合法的ID值！")
                                  @PathVariable Long id) {
        log.debug("开始处理【根据id获取SPU详情】的请求，参数id：{}", id);
        SpuStandardVO standardVO = spuService.getStandardById(id);
        return JsonResult.ok(standardVO);
    }

    // http://localhost:9180/spu/fullInfo/{id}
    @GetMapping("/fullInfo/{id:^\\d+$}")
    @ApiOperation("根据id获取SPU完整信息")
    @ApiOperationSupport(order = 410)
    public JsonResult getFullInfo(@Range(min = 1, message = "请输入合法的ID值！")
                                  @PathVariable Long id) {
        log.debug("开始处理【根据id获取SPU完整信息】的请求，参数id：{}", id);
        SpuFullInfoVO spuFullInfo = spuService.getFullInfoById(id);
        return JsonResult.ok(spuFullInfo);
    }

    // http://localhost:9180/spu/edit/{id}
    @GetMapping("/edit/{id:^\\d+$}")
    @ApiOperation("根据id查询编辑对话框的SPU信息")
    @ApiOperationSupport(order = 420)
    public JsonResult getEditInfo(@Range(min = 1, message = "请输入合法的ID值！")
                                  @PathVariable Long id) {
        log.debug("开始处理【根据id查询编辑对话框的SPU信息】的请求，参数id：{}", id);
        SpuEditListVO editInfo = spuService.getEditInfo(id);
        return JsonResult.ok(editInfo);
    }

    // http://localhost:9180/spu?page=1
    // http://localhost:9180/spu?queryType=all
    @GetMapping("")
    @ApiOperation("查询SPU列表")
    @ApiOperationSupport(order = 430)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "long"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")
    })
    public JsonResult list(@Range(min = 1, message = "请输入合法的ID值！") Integer page, String queryType) {
        log.debug("开始处理【查询SPU列表】的请求，页码：{}", page);
        page = page == null ? 1 : page;
        PageData<SpuListItemVO> pageData;
        if ("all".equals(queryType)) {
            pageData = spuService.getList(page, Integer.MAX_VALUE);
        } else {
            pageData = spuService.getList(page);
        }
        return JsonResult.ok(pageData);
    }

    // http://localhost:9180/spu/{spuId}/sku-list
    @GetMapping("/{id:^\\d+$}/sku-list")
    @ApiOperation("根据SPU_ID查询SKU列表")
    @ApiOperationSupport(order = 440)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "SPU_ID", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")
    })
    public JsonResult list(@Range(min = 1, message = "查询相册列表失败，请提供正确的ID值！") @PathVariable Long id,
                           @Range(min = 1, message = "查询相册列表失败，请提供正确的页码值！") Integer page,
                           @Ignore @AuthenticationPrincipal LoginPrincipal loginPrincipal, String queryType) {
        log.debug("开始处理【根据SPU_ID查询SKU列表】请求，SUP_ID:{},页码：{}", id,page);
        log.debug("当事人：{}", loginPrincipal);
        if (page == null || page < 1) {
            page = 1;
        }
        PageData<SkuListItemVO> pageData;
        if ("all".equals(queryType)) {
            pageData = spuService.getSkuListBySpuList(id, page, Integer.MAX_VALUE);
        } else {
            pageData = spuService.getSkuListBySpuList(id, page, defaultQuerySize);
        }
        return JsonResult.ok(pageData);
    }
}
